package com.ayf.payment.game.api.controller.ayb;

import cn.hutool.core.util.IdcardUtil;
import com.ayf.payment.game.api.channel.alipay.internal.util.StringUtils;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.ayb.MerchantTwoDTO;
import com.ayf.payment.game.api.dto.ayb.SignPayProductDTO;
import com.ayf.payment.game.api.dto.ayb.SignPayProductDTO2;
import com.ayf.payment.game.api.dto.criteria.SignPayProductCriteria;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.SignPayProductService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CheckUtils;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 二级商户管理
 */
@RequestMapping("/v/ayb")
@RestController
public class MerchantTwoController {

    @Autowired
    private SignPayProductService signPayProductService;

    /**
     * 二级商户申请
     * @return
     */
    @RequestMapping("/merchant_apply")
    public CommonResult<?> merchantApply(MerchantDTO merchantDTO,@RequestBody SignPayProductDTO signPayProductDTO) {

        if (signPayProductDTO == null || signPayProductDTO.getMerchantTwoDTO()==null) {
            throw new ParameterException("申请信息不能为空");
        }
        signPayProductDTO.setMerchantId(merchantDTO.getId());
        MerchantTwoDTO merchantTwoDTO=signPayProductDTO.getMerchantTwoDTO();
        if (StringUtils.isEmpty(merchantTwoDTO.getSignedName())) {
            throw new ParameterException("姓名不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getZzLegalPersonIdNo())) {
            throw new ParameterException("身份证号不能为空");
        }
        if (!IdcardUtil.isValidCard(merchantTwoDTO.getZzLegalPersonIdNo())) {
            throw new ParameterException("身份证号格式错误");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getPhone())) {
            throw new ParameterException("手机号不能为空");
        }
        if (!CheckUtils.checkPhone(merchantTwoDTO.getPhone())) {
            throw new ParameterException("手机号格式错误");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressProvince())) {
            throw new ParameterException("所在省不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressCity())) {
            throw new ParameterException("所在市不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressArea())) {
            throw new ParameterException("所在区不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddress())) {
            throw new ParameterException("详细地址不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getAccountName())) {
            throw new ParameterException("开户名称不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBankCardNo())) {
            throw new ParameterException("银行卡号不能为空");
        }
        if (!CheckUtils.checkBankCard(merchantTwoDTO.getBankCardNo())) {
            throw new ParameterException("银行卡号错误");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getProvinceCode())) {
            throw new ParameterException("开户省不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getCityCode())) {
            throw new ParameterException("开户市不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getLegalIdCardProsPath())) {
            throw new ParameterException("身份证正面照不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getLegalIdCardConsPath())) {
            throw new ParameterException("身份证背面照不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getLegalPersonBankCardPath())) {
            throw new ParameterException("银行卡正面照不能为空");
        }
        merchantTwoDTO.setProfession("其他类");
        return null;
    }

    @RequestMapping("/merchant_modify_info")
    public CommonResult<?> merchantModifyInfo(MerchantDTO merchantDTO,@RequestBody MerchantTwoDTO merchantTwoDTO) {
        if (merchantTwoDTO == null) {
            throw new ParameterException("申请信息不能为空");
        }
        SignPayProductDTO signPayProductDTO=new SignPayProductDTO();
        signPayProductDTO.setMerchantId(merchantDTO.getId());

        if (StringUtils.isEmpty(merchantTwoDTO.getPhone())) {
            throw new ParameterException("手机号不能为空");
        }
        if (!CheckUtils.checkPhone(merchantTwoDTO.getPhone())) {
            throw new ParameterException("手机号格式错误");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getProfession())) {
            throw new ParameterException("职业不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressProvince())) {
            throw new ParameterException("所在省不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressCity())) {
            throw new ParameterException("所在市不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddressArea())) {
            throw new ParameterException("所在区不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getBusinessAddress())) {
            throw new ParameterException("详细地址不能为空");
        }
        signPayProductDTO.setMerchantTwoDTO(merchantTwoDTO);
        return null;

    }

    @RequestMapping("/merchant_modify_bank")
    public CommonResult<?> merchantModifyBank(MerchantDTO merchantDTO,@RequestBody MerchantTwoDTO merchantTwoDTO) {
        if (merchantTwoDTO == null) {
            throw new ParameterException("申请信息不能为空");
        }
        SignPayProductDTO signPayProductDTO=new SignPayProductDTO();
        signPayProductDTO.setMerchantId(merchantDTO.getId());

        if (StringUtils.isEmpty(merchantTwoDTO.getBankCardNo())) {
            throw new ParameterException("银行卡号不能为空");
        }
        if (!CheckUtils.checkBankCard(merchantTwoDTO.getBankCardNo())) {
            throw new ParameterException("银行卡号错误");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getProvinceCode())) {
            throw new ParameterException("开户省不能为空");
        }
        if (StringUtils.isEmpty(merchantTwoDTO.getCityCode())) {
            throw new ParameterException("开户市不能为空");
        }
        signPayProductDTO.setMerchantTwoDTO(merchantTwoDTO);
        return null;
    }

    @RequestMapping("/merchant_qurey")
    public CommonResult<?> merchantQurey(MerchantDTO merchantDTO) {
        SignPayProductDTO signPayProductDTO=new SignPayProductDTO();
        signPayProductDTO.setMerchantId(merchantDTO.getId());
        return null;
    }

    /**
     * 查询二级商户列表
     *
     * @param merchantDTO
     * @param selectCriteria
     * @return
     */
    @PostMapping("/selectPageSignPayProduct")
    public CommonResult<PageInfo> selectPageSignPayProduct(MerchantDTO merchantDTO, @RequestBody SignPayProductCriteria selectCriteria) {
        PageHelper.startPage(selectCriteria.getPageNum(), selectCriteria.getPageSize());
        List<SignPayProductDTO2> signPayProductDTOS = signPayProductService.selectPageSignPayProduct(selectCriteria);
        PageInfo pageInfo = new PageInfo(signPayProductDTOS);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, pageInfo);
    }

    /**
     * 查询二级商户详情
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/getSignPayProductDetail")
    public CommonResult<SignPayProductDTO> getSignPayProductDetail(MerchantDTO merchantDTO,@RequestBody SignPayProductCriteria selectCriteria) {
        SignPayProductDTO signPayProductDTO = signPayProductService.getSignPayProductDTOByMId(merchantDTO.getId(),selectCriteria.getPayCode());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, signPayProductDTO);
    }


}
